//
//  HomeTodayController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/17.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "HomeTodayController.h"
#import "PWNetWorking.h"
#import "ShardModel.h"
#import "ShardCell.h"
#import "TodayModel.h"
#import "TodayCell.h"
#import "UIImageView+WebCache.h"
#import "TodayDetailViewController.h"
#import "ShardDetailViewController.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface HomeTodayController ()<UITableViewDelegate, UITableViewDataSource>
// 碎片
@property (nonatomic, retain) UITableView *shardTableView;
@property (nonatomic, retain) NSMutableArray *shardDataArr;
@property (nonatomic, retain) UIBarButtonItem *shardItem;
// 今日推荐
@property (nonatomic, retain) UIBarButtonItem *todayItem;
@property (nonatomic, retain) UITableView *todayTableView;
@property (nonatomic ,retain) NSMutableArray *todayDataArr;
// 动态
@property (nonatomic, retain) UIBarButtonItem *dynamicItem;
@property (nonatomic, retain) UIScrollView *backgroundSV;

@end

@implementation HomeTodayController

- (void)dealloc
{
    [_shardItem release];
    [_shardTableView release];
    [_shardDataArr release];
    [_todayItem release];
    [_todayTableView release];
    [_todayDataArr release];
    [_dynamicItem release];
    [_backgroundSV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createnavigationBarButton];
    [self createBackgroundScrollView];
    [self createTableView];
    [self dataHandler];
    // 导航栏按钮 下方线view
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(268, 0, 27, 5)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.tag = 1001;
    [self.view addSubview:lineView];
    [lineView release];
    
}
// 创建navigationBarButton
- (void)createnavigationBarButton
{
    // 导航栏按钮
    self.shardItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_suipian.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    _shardItem.tag = 2000;
    self.todayItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_today.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    _todayItem.tag = 2001;
    self.dynamicItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_dongtai.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    _dynamicItem.tag = 2002;
    /**< item颜色 */
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    // 添加右按钮
    self.navigationItem.rightBarButtonItems = @[ _dynamicItem, _todayItem, _shardItem];
}
// 导航栏按钮触发方法
- (void)scrollAction:(UIBarButtonItem *)btn
{
   
    [_backgroundSV setContentOffset:CGPointMake(WIDTH *(btn.tag - 2000), 0) animated:YES];

//    for (UIBarButtonItem *btn in self.navigationItem.rightBarButtonItems) {
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    }
//    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
    
    
}


// backgroundScollView
- (void)createBackgroundScrollView
{
    CGRect f =  self.view.frame;
    f.origin.y += 5;
    f.size.height -= 5;
    self.backgroundSV = [[UIScrollView alloc] initWithFrame:f];
    _backgroundSV.backgroundColor = [UIColor blueColor];
    _backgroundSV.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _backgroundSV.pagingEnabled = YES;
    _backgroundSV.showsHorizontalScrollIndicator = NO;
    _backgroundSV.showsVerticalScrollIndicator = NO;
    _backgroundSV.bounces = NO;

    [self.view addSubview:_backgroundSV];
    [_backgroundSV release];
    
}
// 创建todayTableView
- (void)createTableView
{
    self.todayTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _todayTableView.dataSource = self;
    _todayTableView.delegate = self;
    [_backgroundSV addSubview:_todayTableView];
    [_todayTableView release];
//    _tableView.tableHeaderView =
    [_todayTableView registerClass:[TodayCell class] forCellReuseIdentifier:@"todayCell"];
    _todayTableView.showsHorizontalScrollIndicator = NO;
    _todayTableView.showsVerticalScrollIndicator = NO;
    
    self.shardTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width , self.view.bounds.size.height)];
    _shardTableView.backgroundColor = [UIColor redColor];
    [self.backgroundSV addSubview:_shardTableView];
    _shardTableView.delegate = self;
    _shardTableView.dataSource = self;
    [_shardTableView release];
    
    [_shardTableView registerClass:[ShardCell class] forCellReuseIdentifier:@"shardcell"];
    
}
- (void)dataHandler
{
    NSString *todayStr = @"http://api2.pianke.me/pub/today";
    // auth=&client=1&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&limit=10&start=0&version=3.0.6
    NSDictionary *tBodyDic = @{@"auth":@"",@"client":@"1", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"limit": @"10", @"start": @"0", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:todayStr body:tBodyDic completion:^(id result) {
        self.todayDataArr = [TodayModel modelHanderWithArray:result[@"data"][@"list"]];
//        NSLog(@"%@", result);
        [_todayTableView reloadData];
        
    }];
    NSString *shardStr = @"http://api2.pianke.me/timeline/list";
    NSDictionary *shardBodyDic = @{@"auth": @"C5VuFSS49v9DCsJzHMbslDJxQMqvZraIm325tCj1lcWP229VPSRVc7f4", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"limit": @"10", @"start": @"0", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:shardStr body:shardBodyDic completion:^(id result) {
        self.shardDataArr = [ShardModel modelHanderWithArray:result[@"data"][@"list"]];
        //        NSLog(@"%@", result);
        [_shardTableView reloadData];
        
    }];
    
}
#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _todayTableView) {
        return _todayDataArr.count;
    } else if(tableView ==_shardTableView)
    {
        return _shardDataArr.count;
    }else
        return 10;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _todayTableView) {
        TodayCell *todayCell = [tableView dequeueReusableCellWithIdentifier:@"todayCell" forIndexPath:indexPath];
        TodayModel *tM = _todayDataArr[indexPath.row];
        todayCell.nameLabel.text = tM.name;
        todayCell.ennameLabel.text = tM.enname;
        //[cell.coverImg sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
        [todayCell.coverimg sd_setImageWithURL:[NSURL URLWithString:tM.coverimg]];
        todayCell.titleLabel.text = tM.title;
        todayCell.contentLabel.text = tM.content;
        todayCell.likeCount.text = tM.like.stringValue;
        return todayCell;
    } else {
        ShardCell *shardCell = [tableView dequeueReusableCellWithIdentifier:@"shardcell" forIndexPath:indexPath];
        ShardModel *sM = _shardDataArr[indexPath.row];
        [shardCell.iconImg sd_setImageWithURL:[NSURL URLWithString:sM.icon]];
        shardCell.nameLabel.text = sM.uname;
        shardCell.timeLabel.text = sM.addtime_f;
        shardCell.coverImg_wh = sM.coverimg_wh;
        [shardCell.coverImg sd_setImageWithURL:[NSURL URLWithString:sM.coverimg]];
        shardCell.contentLabel.text = sM.content;
        shardCell.commentCount.text = [sM.comment stringValue];
        shardCell.likeCount.text = [sM.like stringValue];
        shardCell.tagLabel.text = sM.tag;
        shardCell.tagCount.text = [sM.count stringValue];
        return shardCell;
        
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _todayTableView) {
        TodayModel *tM = _todayDataArr[indexPath.row];
        CGFloat textH = [TodayCell heightWithText:tM.content];
        return 270 + textH;
    } else  if(tableView == _shardTableView){
        ShardModel *sM = _shardDataArr[indexPath.row];
        CGFloat textH = [ShardCell heightWithText:sM.content];
        if (sM.coverimg.length) {
            CGFloat imgH = [ShardCell heightWithImage:sM.coverimg_wh];
            return 150 + imgH + textH;
        }else{
            return 150 + textH;
        }
    } else {
        return 10;
    }
}
// 跳转到详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _todayTableView) {
        TodayDetailViewController *todayDetailVC = [[TodayDetailViewController alloc] init];
        [self.navigationController pushViewController:todayDetailVC animated:YES];

        
    } else  if(tableView == _shardTableView){
        ShardModel *shardModel = _shardDataArr[indexPath.row];
        ShardDetailViewController *shardDetailVC = [[ShardDetailViewController alloc] init];
        shardDetailVC.contentid = shardModel.contentid;
        // 属性传值
        shardDetailVC.model = shardModel;
        [self.navigationController pushViewController:shardDetailVC animated:YES];
    
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
